#include "input.h"
#include <cstdio>
#include <cstring>
#include <iostream>
#include <unordered_set>
#include "error.h"
#include <fstream>
#include <string>

using namespace std;

unordered_set<string> s;

int getWords(char *fileName, char *word[], int *length) {
    FILE *fp;
    fopen_s(&fp, fileName, "r");

    if (fp == nullptr) {
        cerr << "单词表所在文件不存在！NO SUCH FILE!" << endl;
        return -Error::FILE_NOT_EXIST;
    }
    else {
        string str;
        str = "";
        int c;
        int cnt = 0;
        s.clear();
        while ((c = fgetc(fp)) != EOF) {
            if (c >= 'A' && c <= 'Z')
                str += char(c - 'A' + 'a');
            else if (c >= 'a' && c <= 'z')
                str += (char) c;
            else {
                if (str.size() > 1) {
                    if (s.count(str) == 0) {
                        char *tmp = (char *) malloc(str.length() + 1);
                        int len = (int) str.size();
                        str.copy(tmp, len, 0);
                        *(tmp + len) = '\0';
                        word[++cnt] = tmp;
                        s.insert(str);
                    }
                }
                str = "";
            }
        }
        if (str.size() > 1) {
            if (s.count(str) == 0) {
                char *tmp = (char *) malloc(str.length() + 1);
                int len = (int) str.size();
                str.copy(tmp, len, 0);
                *(tmp + len) = '\0';
                word[++cnt] = tmp;
                s.insert(str);
            }
        }
        *length = cnt;
        fclose(fp);
        return 0;
    }

}